package com.group4.xiao_ge_lai_yi_zhu.dao;

import com.group4.xiao_ge_lai_yi_zhu.controller.login_controller.LoginPage;
import com.group4.xiao_ge_lai_yi_zhu.domain.Lottery;
import com.group4.xiao_ge_lai_yi_zhu.domain.User;

import java.io.*;
import java.util.HashMap;
import java.util.Properties;

/**
 * 用户数据类
 * 1.使用容器暂时存储用户数据
 * 2.将数据存到本地
 */
public class UserDAO {
    private static Properties properties = new Properties();
    private User user = new User();

    //根据当前用户名去找文件返回序号,彩票号码
    public HashMap<String, String> findLottery() {
        HashMap<String, String> hashMap = new HashMap<>();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("main_interface/用户购买信息/"+ LoginPage.getUsername+ ".txt"));
//            br = new BufferedReader(new FileReader("main_interface/用户购买信息/baiyan.txt"));
            String line;
            while ((line = br.readLine()) != null) {
                String[] split = line.split(",");
                hashMap.put(split[0], split[3]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return hashMap;
    }

    //开奖后将号码放到往期号码里


    //向本地文件上传彩票
    public boolean lotteryUpload(Lottery lottery) {
        String lotteryContent = lottery.getSerialNum() + "," + lottery.getPeriod() + "," + lottery.getPrizeTime() + "," + lottery.getLotteryNum() + "," + lottery.getTicketTime();
        BufferedWriter fw = null;
        BufferedWriter bfw = null;
        try {
            //当前用户名可改
            fw = new BufferedWriter(new FileWriter("main_interface/用户购买信息/" + LoginPage.getUsername + ".txt", true));
            bfw = new BufferedWriter(new FileWriter("main_interface/用户购买信息/Everyone.txt", true));
            fw.write(lotteryContent);
            bfw.write(lotteryContent);
            bfw.newLine();
            fw.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bfw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * 判断用户名是否注册 false已注册 true没注册
     *
     * @param username
     * @return
     */
    public boolean usernameExist(String username) {
        FileReader fr = null;
        try {
            fr = new FileReader("main_interface/userinfo.properties");
            properties.load(fr);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //2.获取集合中的键数据,对比用户名是否相同
        String name = properties.getProperty(username);
        return name == null || "".equals(name);
    }

    //保存用户信息
    public boolean saveUserinfo(User user) {
        FileWriter fw = null;
        try {
            fw = new FileWriter("main_interface/userinfo.properties", true);
            fw.write(user.getUsername());
            fw.write("=");
            fw.write(user.getPassword());
            fw.write("\r\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        String name = properties.getProperty(user.getUsername());
        return name == null || "".equals(name);
    }

    //判断密码是否存在
    public boolean passwordExist(String username, String password) {
        FileReader fr = null;
        try {
            fr = new FileReader("main_interface/userinfo.properties");
            properties.load(fr);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //2.获取集合中的键值对数据,封装到学生对象中
        String name = properties.getProperty(username);
        return password.equals(name);
    }
}